// pages/usermanager/usermanager.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    placeHolder: '输入你要查找的内容',
    autoFocus: false,
    inputEnable: true,
    keydown_number: 0,
    input_value: "",
    usermsg: true,
  },
  skip: 0,

    redictAct: function (event) {
      var id = event.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../usermanager2/usermanager2?id=' + id
      });
    },
    // 获取滚动条当前位置
    onPageScroll: function (e) {
     // console.log(e)
      if (e.scrollTop > 70) {
        this.setData({
          floorstatus: true
        });
      } else {
        this.setData({
          floorstatus: false
        });
      }
    },
    //回到顶部
    goTop: function (e) {
      if (wx.pageScrollTo) {
        wx.pageScrollTo({
          scrollTop: 0
        })
      }
    },

    getData: function (skip) { 
      const db = wx.cloud.database()
      db.collection("book_user")
        .count().then(res => {
          this.setData({
            usersum: res.total,
          })
            const db = wx.cloud.database()
            db.collection("book_user")
            .skip(skip)
              .get({
                success: res => {
                  this.skip +=20
                  this.data.list = this.data.list.concat(res.data)
                    this.setData({
                      list : this.data.list,
                    })
                },
                fail: err => {
                  console.log(err)
                }
              })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
    },
    onShow: function () {
      this.setData({
        list :[]
      })
      this.getData(this.skip=0)
    },

    onReachBottom: function () {
      wx.showLoading({
        title: '正在加载',
        duration: 1000
      })
      this.getData(this.skip)
    },
    onInput: function (e) {
      this.setData({
        searchKey: e.detail.value,
      })
      if (e.detail.cursor != 0) {
        this.setData({
          keydown_number: 1
        })
      } else {
        this.setData({
          keydown_number: 0
        })
      }
    },
    formSubmit: function (e) {
      if (this.data.keydown_number == 1) {
        this.setData({
          key: this.data.searchKey
        })
        const db = wx.cloud.database();
        const _ = db.command
        db.collection("book_user")
          .where(_.or([{
              name: db.RegExp({
                regexp: '.*' + this.data.key,
                options: 'i',
              })
            },
            {
              grade: db.RegExp({
                regexp: '.*' + this.data.key,
                options: 'i',
              })
            },
            {
              sex: db.RegExp({
                regexp: '.*' + this.data.key,
                options: 'i',
              })
            }
          ]))
          .get({
            success: res => {
              this.setData({
                list: res.data,
                usersum: res.data.length,
              })
              console.log(res.data.length)
              if (this.data.list.length == 0) {
                wx.showToast({
                  icon: 'none',
                  title: '没找到相关用户',
                  duration: 2000
                })
                this.setData({
                  usermsg: false,
                })
              } else {
                this.setData({
                  usermsg: true,
                })
              }
            },
            fail: err => {
              console.log(err)
            }
          })
      } else {
        wx.showModal({
          title: '提示',
          content: '请输入内容',
          showCancel: false,
        });
      }
    },
    onClear: function () {
      this.setData({
        searchKey: '',
        usermsg: false
      })

    },

})